package org.jeecg.modules.basic.demo.apartment.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.basic.demo.apartment.entity.LwgyBaseApartment;
import org.jeecg.modules.basic.demo.apartment.entity.LwgyBaseApartmentTreeList;
import org.jeecg.modules.basic.demo.apartment.entity.LwgyRoomViews;
import org.jeecg.modules.basic.demo.apartment.service.ILwgyBaseApartmentService;
import org.jeecg.modules.basic.demo.apartment.service.ILwgyRoomViewsService;
import org.jeecg.modules.basic.demo.bed.entity.LwgyBaseBed;
import org.jeecg.modules.basic.demo.bed.mapper.LwgyBaseBedMapper;
import org.jeecg.modules.basic.demo.bed.service.ILwgyBaseBedService;
import org.jeecg.modules.basic.demo.school.entity.SchoolTreeModel;
import org.jeecg.modules.basic.demo.student.entity.LwgyBaseStudent;
import org.jeecg.modules.basic.demo.student.mapper.LwgyBaseStudentMapper;
import org.jeecg.modules.basic.demo.student.service.ILwgyBaseStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.awt.event.PaintEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * TODO
 *
 * @author CSD
 * @date 2021-04-27 16:10
 */
@Api(tags = "公寓房间视图")
@RestController
@RequestMapping("/apartment/lwgyRoomViews")
@Slf4j
public class LwgyRoomViewsController extends JeecgController<LwgyRoomViews, ILwgyRoomViewsService> {

    @Autowired
    private ILwgyRoomViewsService lwgyRoomViewsService;

    @Autowired
    private ILwgyBaseApartmentService lwgyBaseApartmentService;

    @Resource
    private LwgyBaseStudentMapper lwgyBaseStudentMapper;
    @Resource
    private ILwgyBaseBedService lwgyBaseBedService;



    @ApiOperation(value = "房间列表")
    @GetMapping("/roomList")
    public Result<?>roomList(LwgyRoomViews lwgyRoomViews, HttpServletRequest req){
        return Result.OK(roomList1(lwgyRoomViews));
    }

    private List<List<LwgyRoomViews>> roomList1(LwgyRoomViews lwgyRoomViews){
        List<List<LwgyRoomViews>>lwgyRoomLists = new ArrayList<>(4);
        List<String> lists = lwgyBaseApartmentService.getRoomType(lwgyRoomViews.getLayerId());

        for (String list1 : lists) {
            lwgyRoomViews.setRoomOrientation(list1);
            QueryWrapper<LwgyRoomViews> queryWrapper = QueryGenerator.initQueryWrapper(lwgyRoomViews, null);
            queryWrapper.orderByAsc("room_name");

            List<LwgyRoomViews> list = lwgyRoomViewsService.list(queryWrapper);
            lwgyRoomLists.add(list);
        }
        List<String> strings = new ArrayList<>();
        for (List<LwgyRoomViews> lwgyRoomList : lwgyRoomLists) {
            for (LwgyRoomViews roomViews : lwgyRoomList) {
                strings.add(roomViews.getRoomId());
            }
        }
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<String> role = lwgyBaseBedService.getRoleById(sysUser.getId());

        QueryWrapper<LwgyBaseBed> bedQueryWrapper = new QueryWrapper<>();
        if (!role.contains("admin")) {//不是管理员，显示管理员分配的的床位
            String workNo = sysUser.getWorkNo();
            bedQueryWrapper.eq("admin_id",workNo);
        }
        bedQueryWrapper.in("room_id",strings);
        bedQueryWrapper.orderByAsc("bed_code");
        List<LwgyBaseBed> bedList = lwgyBaseBedService.list(bedQueryWrapper);
        for (List<LwgyRoomViews> lwgyRoomList : lwgyRoomLists) {
            Iterator<LwgyRoomViews> iterator = lwgyRoomList.iterator();
            while (iterator.hasNext()){
                LwgyRoomViews roomViews = iterator.next();
//            }
//            for (LwgyRoomViews roomViews : lwgyRoomList) {
                List<LwgyBaseBed> lwgyBaseBeds = new ArrayList<>(6);
                for (LwgyBaseBed lwgyBaseBed : bedList) {
                    //List<LwgyRoomViews> collect = lwgyRoomList.stream().filter(roomViews1 -> roomViews1.getRoomId().equals(lwgyBaseBed.getRoomId())).collect(Collectors.toList());
                    if(roomViews.getRoomId().equals(lwgyBaseBed.getRoomId())){
                        lwgyBaseBeds.add(lwgyBaseBed);
                    }
                }
                roomViews.setLwgyBaseBeds(lwgyBaseBeds);
            }
        }
        return lwgyRoomLists;
    }

    @ApiOperation(value = "楼层列表->房间列表")
    @GetMapping("/layerList")
    public Result<?>layerList(LwgyRoomViews lwgyRoomViews, HttpServletRequest req){
        return Result.OK(layerListByUnitId(lwgyRoomViews));
    }

    @ApiOperation(value = "单元集合->楼层集合->房间集合")
    @GetMapping("/unitList")
    public Result<?>unitList(LwgyRoomViews lwgyRoomViews, HttpServletRequest req){
        return Result.OK(unitLayerByBuilding(lwgyRoomViews));
    }

    @ApiOperation(value = "楼宇 --> 单元集合 --> 楼层集合 --> 房间集合")
    @GetMapping("/buildingList")
    public Result<?>buildingList(LwgyRoomViews lwgyRoomViews, HttpServletRequest req){
        return Result.OK(buildingListByCampus(lwgyRoomViews));
    }

    @ApiOperation(value = "校区 --> 楼宇 --> 单元集合 --> 楼层集合 --> 房间集合")
    @GetMapping("/campusList")
    public Result<?>campusList(LwgyRoomViews lwgyRoomViews, HttpServletRequest req){
        return Result.OK(campusListBySchool(lwgyRoomViews));
    }

    /**
     * 楼层列表 --> 房间列表
     * @param lwgyRoomViews
     * @return
     */
    private List<List<List<LwgyRoomViews>>>layerListByUnitId(LwgyRoomViews lwgyRoomViews){
        List<String> lists = lwgyBaseApartmentService.getChildrenIdByPid(lwgyRoomViews.getUnitId());
        List<List<List<LwgyRoomViews>>> layerLists = new ArrayList<>(6);
       /* for (String list : lists) {
            LwgyRoomViews lwgyRoomViews1 = new LwgyRoomViews();
            lwgyRoomViews1.setLayerId(list);
            List<List<LwgyRoomViews>> lists1 = roomList1(lwgyRoomViews1);
            layerLists.add(lists1);
        }*/
        lists.stream().forEach(item->{
            LwgyRoomViews lwgyRoomViews1 = new LwgyRoomViews();
            lwgyRoomViews1.setLayerId(item);
            List<List<LwgyRoomViews>> lists1 = roomList1(lwgyRoomViews1);
            layerLists.add(lists1);
        });
        return layerLists;
    }

    /**
     * 单元集合 --> 楼层集合 --> 房间集合
     * @param lwgyRoomViews
     * @return
     */
    private List<List<List<List<LwgyRoomViews>>>>unitLayerByBuilding(LwgyRoomViews lwgyRoomViews){
        List<String> lists = lwgyBaseApartmentService.getChildrenIdByPid(lwgyRoomViews.getBuildingId());
        List<List<List<List<LwgyRoomViews>>>> unitLists = new ArrayList<>(3);
        for (String list : lists) {
            LwgyRoomViews lwgyRoomViews1 = new LwgyRoomViews();
            lwgyRoomViews1.setUnitId(list);
            List<List<List<LwgyRoomViews>>> lists1 = layerListByUnitId(lwgyRoomViews1);
            unitLists.add(lists1);
        }
        return unitLists;
    }

    /**
     * 楼宇 --> 单元集合 --> 楼层集合 --> 房间集合
     * @param lwgyRoomViews
     * @return
     */
    private List<List<List<List<List<LwgyRoomViews>>>>>buildingListByCampus(LwgyRoomViews lwgyRoomViews){
        List<String> lists = lwgyBaseApartmentService.getChildrenIdByPid(lwgyRoomViews.getCampusId());
        List<List<List<List<List<LwgyRoomViews>>>>>buildingListByCampus = new ArrayList<>(20);
        for (String list : lists) {
            LwgyRoomViews lwgyRoomViews1 = new LwgyRoomViews();
            lwgyRoomViews1.setBuildingId(list);
            List<List<List<List<LwgyRoomViews>>>> lists1 = unitLayerByBuilding(lwgyRoomViews1);
            buildingListByCampus.add(lists1);
        }
        return buildingListByCampus;
    }    /**
     * 校区 --> 楼宇 --> 单元集合 --> 楼层集合 --> 房间集合
     * @param lwgyRoomViews
     * @return
     */
    private List<List<List<List<List<List<LwgyRoomViews>>>>>>campusListBySchool(LwgyRoomViews lwgyRoomViews){
        List<String> lists = lwgyBaseApartmentService.getChildrenIdByPid("0");
        List<List<List<List<List<List<LwgyRoomViews>>>>>> campusList = new ArrayList<>(20);
        for (String campusId : lists) {
            LwgyRoomViews lwgyRoomViews1 = new LwgyRoomViews();
            lwgyRoomViews1.setCampusId(campusId);
            List<List<List<List<List<LwgyRoomViews>>>>> lists1 = buildingListByCampus(lwgyRoomViews1);
            campusList.add(lists1);
        }
        return campusList;
    }




    @ApiOperation(value = "房间列表")
    @GetMapping("/list")
    public Result<?>list(LwgyRoomViews lwgyRoomViews, HttpServletRequest req){
        QueryWrapper<LwgyRoomViews> queryWrapper = QueryGenerator.initQueryWrapper(lwgyRoomViews, req.getParameterMap());
        queryWrapper.orderByAsc("room_name");
        List<LwgyRoomViews> list = lwgyRoomViewsService.list(queryWrapper);
        list.forEach(System.out::println);
        return Result.OK(list);
    }
    @ApiOperation(value = "房间列表树形结构")
    @RequestMapping(value = "/queryTreeList", method = RequestMethod.GET)
    public Result<List<LwgyBaseApartmentTreeList>> queryTreeList(String type,String id) {
        Result<List<LwgyBaseApartmentTreeList>> result = new Result<>();
        List<LwgyBaseApartmentTreeList> list = lwgyBaseApartmentService.queryTreeList(type,id);
        result.setResult(list);
        result.setSuccess(true);

        return result;
    }

    @ApiOperation(value = "根据studentId获取学生信息")
    @GetMapping(value = "/getStuInfoByStuId/{studentId}")
    public Result<?>getStuInfoByStuId(@PathVariable String studentId){
        QueryWrapper<LwgyBaseStudent> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.select("id","student_number","student_name","student_sex","student_college","student_major","student_class","student_phone","student_year","student_education","student_education","student_teacher");
        studentQueryWrapper.eq("id",studentId);
        LwgyBaseStudent lwgyBaseStudent = lwgyBaseStudentMapper.selectList(studentQueryWrapper).get(0);
        return Result.OK(lwgyBaseStudent);
    }

}
